package com.ai.databao.service.impl;

import com.ai.databao.common.util.MDUtil;
import com.ai.databao.common.util.StringUtil;
import com.ai.databao.utils.HttpClient;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.message.BasicNameValuePair;

import java.util.*;

/**
 * Created by fangyunfeng on 2017/8/3.
 */
public abstract class AbstractLmzxQuery {
    //定义共同参数
    //public static final String apiUrl = "https://api.limuzhengxin.com/api/gateway";//服务地址---需客户指定---需联系立木开启IP白名单
    public static final String apiUrl = "https://t.limuzhengxin.cn/api/gateway";//服务地址---需客户指定---需联系立木开启IP白名单
    public static final String apiKey = "9285208412083271";//API授权 key ---需客户指定
    public static final String apiSecret = "10WH4MPzX9Ac9wr9XmeSO2QBzCH5uV21";//API授权 secret---需客户指定

    public final String version = "1.2.0";//API版本号

    /**
     * 签名转化
     *
     * @param reqParam
     * @return
     */
    public String getSign(List<BasicNameValuePair> reqParam) {

        StringBuffer sbb = new StringBuffer();
        int index = 1;
        for (BasicNameValuePair nameValuePair : reqParam) {
            sbb.append(nameValuePair.getName() + "=" + nameValuePair.getValue());
            if (reqParam.size() != index) {
                sbb.append("&");
            }
            index++;
        }
        String sign = sbb.toString();

        Map<String, String> paramMap = new HashMap<String, String>();
        String ret = "";
        if (!StringUtil.isEmpty(sign)) {
            String[] arr = sign.split("&");
            for (int i = 0; i < arr.length; i++) {
                String tmp = arr[i];
                if (-1 != tmp.indexOf("=")) {
                    paramMap.put(tmp.substring(0, tmp.indexOf("=")), tmp.substring(tmp.indexOf("=") + 1, tmp.length()));
                }

            }
        }
        List<Map.Entry<String, String>> list = new ArrayList<>(paramMap.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                int ret = 0;
                ret = o1.getKey().compareTo(o2.getKey());
                if (ret > 0) {
                    ret = 1;
                } else {
                    ret = -1;
                }
                return ret;
            }
        });

        StringBuilder sbTmp = new StringBuilder();
        for (Map.Entry<String, String> mapping : list) {
            if (!"sign".equals(mapping.getKey())) {
                sbTmp.append(mapping.getKey()).append("=").append(mapping.getValue()).append("&");
            }
        }
        sbTmp.setLength(sbTmp.lastIndexOf("&"));
        sbTmp.append(apiSecret);
        ret = MDUtil.SHA1(sbTmp.toString());

        //System.out.println(sb.toString());
        return ret;
    }

    /**
     * 调用立木征信接口
     * @param reqParam
     * @return
     */
    public String doPost(List<BasicNameValuePair> reqParam){

        reqParam.add(new BasicNameValuePair("apiKey", apiKey));//API授权
        reqParam.add(new BasicNameValuePair("version", version));//调用的接口版本
        reqParam.add(new BasicNameValuePair("sign", getSign(reqParam)));//请求参数签名

        //提交受理请求
        String json = HttpClient.doPost(apiUrl, reqParam);
        System.out.println("json=" + json);
        return json;
    }

    /**
     * 调用立木征信接口
     * @param reqParams
     * @return
     */
    public String doBizPost(List<BasicNameValuePair> reqParams){
        reqParams.add(new BasicNameValuePair("ts", String.valueOf(System.currentTimeMillis())));//时间戳
        reqParams.add(new BasicNameValuePair("accessType", "api"));//接口类型
        return doPost(reqParams);
    }
}
